﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmNhapdiem : DevExpress.XtraEditors.XtraForm
    {
        DAL.EntitesDAL.BangdiemDAL _dal = new DAL.EntitesDAL.BangdiemDAL();
        DAL.EntitesDAL.KhoahocDAL _dal1 = new DAL.EntitesDAL.KhoahocDAL();
        DAL.EntitesDAL.LophocDAL _dal2 = new DAL.EntitesDAL.LophocDAL();
        DAL.EntitesDAL.HocvienDAL _dal3 = new DAL.EntitesDAL.HocvienDAL();

        BLL.EntitiesBLL.BangdiemBLL _bll = new BLL.EntitiesBLL.BangdiemBLL();

        public FrmNhapdiem()
        {
            InitializeComponent();
        }

        private void FrmNhapdiem_Load(object sender, EventArgs e)
        {
            DataTable _dtb1 = _dal1.Select_Current(DateTime.Now);
            string _id1 = _dtb1.Rows[0]["Makhoa"] + "";
            cboLophoc.Properties.DataSource = _dal2.Select(_id1);

            ReadOnlyControl();            
        }

        private void ReadOnlyControl(bool isTrue = true)
        {
            lbHead.Enabled = isTrue;

            lbStart.Enabled = !isTrue;
            cboHocvien.Properties.ReadOnly = isTrue;
            lbDiemLT.Enabled = !isTrue;
            txtDiemLT.Properties.ReadOnly = isTrue;
            lbDiemTH.Enabled = !isTrue;
            txtDiemTH.Properties.ReadOnly = isTrue;
        }

        private void cboLophoc_EditValueChanged(object sender, EventArgs e)
        {
            ReadOnlyControl(false);
            txtDiemLT.Focus();
            string _id = cboLophoc.EditValue + "";

            DataTable _dtb2 = _dal3.Select_Nhapdiem(_id);
            cboHocvien.Properties.DataSource = _dtb2;
            cboHocvien.ItemIndex = 0;

            DataTable _dtb = _dal.Select(_id);
            if (_dtb != null)
                grcMain.DataSource = _dtb;
        }

        private void btnDongy_Click(object sender, EventArgs e)
        {
            float _diemLT = Convert.ToInt32(txtDiemLT.Text);
            float _diemTH = Convert.ToInt32(txtDiemTH.Text);
            string _xeploai = _bll.Rank(_diemLT, _diemTH);
            var o = new DAL.Entities.Bangdiem()
            {
                MaHVID = cboHocvien.EditValue + "",
                DiemLT = _diemLT,
                DiemTH = _diemTH,
                Xeploai = _xeploai,
                Ghichu = _xeploai=="Rớt" ? "H" : "Đ"
            };

            var ok = _dal.Insert(o);
            if (ok)
            {
                var oke = _dal3.Update(cboHocvien.EditValue + "");
                if (oke)
                {
                    DataTable _dtb2 = _dal3.Select_Nhapdiem(cboLophoc.EditValue + "");
                    cboHocvien.Properties.DataSource = _dtb2;
                    cboHocvien.ItemIndex = 0;

                    DataTable _dtb = _dal.Select(cboLophoc.EditValue + "");
                    if (_dtb != null)
                        grcMain.DataSource = _dtb;
                }
                FrmNhapdiem_Load(sender, e);
                ReadOnlyControl(false);
                btnNhaplai_Click(sender, e);
            }
            else
            {
                MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                ReadOnlyControl(false);
                txtDiemLT.Focus();
            }
        }

        private void btnNhaplai_Click(object sender, EventArgs e)
        {
            txtDiemLT.Text = null;
            txtDiemTH.Text = null;
            txtDiemLT.Focus();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}